package com.cdream.basic.thread;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @author cdream
 * @date 2022/2/23
 */
public class ConcurrentExample {
    private static String chars = "abcdefghijklmnopqrstuvwxyz";

    public static void main(String[] args) {
        // createFile();
        ReentrantLock lock = new ReentrantLock();
        // 可以用引荐的new LongAdder解决
        demo(() -> new ConcurrentHashMap<String,Integer>(), (map, words) -> {
            for (String word : words) {
                // lock.lock();
                Integer count = map.getOrDefault(word, 0);
                map.put(word, count + 1);
                map.computeIfAbsent(word,(key)->{
                    return 1;
                });
                // lock.unlock();
            }
        });
    }

    private static <V> void demo(Supplier<Map<String, V>> supplier, BiConsumer<Map<String, V>, List<String>> consumer) {
        Map<String, V> stringVMap = supplier.get();
        List<Thread> ts = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            int idx = i;
            Thread thread = new Thread(() -> {
                List<String> words = readFromFile(idx);
                consumer.accept(stringVMap, words);
            });
            ts.add(thread);
        }
        ts.forEach(a->a.start());
        ts.forEach(a-> {
            try {
                a.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println(stringVMap);
    }

    private static List<String> readFromFile(int idx) {
        List<String> res = new ArrayList<>();
        try (BufferedReader is = new BufferedReader(new InputStreamReader(new FileInputStream("tmp/" + (idx+1) + ".txt")))) {
            while (true){
                String s = is.readLine();
                if (s==null){
                    break;
                }
                res.add(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }

    private static void createFile() {
        int len = chars.length();
        int count = 200;
        List<String> list = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < count; j++) {
                list.add(String.valueOf(chars.charAt(i)));
            }
        }
        Collections.shuffle(list);
        for (int i = 0; i < 26; i++) {
            try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("tmp/" + (i + 1) + ".txt")))) {
                String collect = list.subList(i * count, (i + 1) * count).stream().collect(Collectors.joining("\n"));
                pw.print(collect);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
